#!/usr/bin/python
#coding:utf-8
#####################################################################
__author__ = "kauu (kauu@yahoo.cn)"
__version__ = "1.0"
__copyright__ = "Copyright (c) 2008 "
__license__ = "NO"


try:
	import urllib2
	import re,os
	import Crawler
	import md5
	import Page
except ImportError:
	raise ImportError('unable to import crawler')	


#para page is an instance of Page
def fetch(page):
	
	crawler = Crawler.Crawler() #get an instanse of Crawler
	try:
		page = crawler.crawlPage(page)
		domain='default'
		meta = page.info.items()
		match = None
		for i in meta:
			print str(i) 
			match = re.search(r'\.(\w+\.com)',str(i))
			if match: 
				domain=match.group(1)
				break
		#print domain	
		if not os.path.isdir(domain):	
			os.mkdir(domain)	
		filename =  md5.new(url).digest()
		save(page,domain+'/'+filename)	
	
	except  str:
		raise RuntimeError("cat't fetch page:",str) 

	


def save(page,file):
	try:
		if not None == page:	
			file = open(file,'w')
			file.writelines(page)
			file.close()
		else:
		    print 'page is None'
	except str:
		print 'except:',str

if __name__ == "__main__":

	url='http://china.hx2car.com/search.htm?query=%B1%A6%C2%ED'
	page = Page()	
	page.url = url
	pp = fetch(page)
		

	
	
	




